package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.data.ConstantEnum;
import com.cw.data.Result;
import com.cw.pojo.TbContent;
import com.cw.pojo.TbContentCategory;
import com.cw.service.ContentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController {

    @Reference
    private ContentService contentService;

    @RequestMapping("toContentCategory")
    public String toContentCategory() {

        return "admin/content_category";
    }

    @RequestMapping("toContent")
    public String toContent(Model model) {

        List<TbContentCategory> categoryList = contentService.getCategory();

        model.addAttribute("cc", categoryList);

        return "admin/content";
    }

    @RequestMapping("getContentCategoryInfo")
    @ResponseBody
    public Result getContentCategoryInfo(String name) {

        try {

            List<TbContentCategory> categoryList = contentService.getContentCategoryInfo(name);

            return Result.result(ConstantEnum.SUCCESS).setData(categoryList);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("addContentCategory")
    @ResponseBody
    public Result addContentCategory(TbContentCategory tbContentCategory) {

        try {

            contentService.addContentCategory(tbContentCategory);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getContentCategory")
    @ResponseBody
    public Result getContentCategory(Long id) {

        try {

            TbContentCategory tbContentCategory = contentService.getContentCategory(id);

            return Result.result(ConstantEnum.SUCCESS).setData(tbContentCategory);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("updateContentCategory")
    @ResponseBody
    public Result updateContentCategory(TbContentCategory tbContentCategory) {

        try {

            contentService.updateContentCategory(tbContentCategory);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("deleteContentCategory")
    @ResponseBody
    public Result deleteContentCategory(Long[] ids) {

        try {

            contentService.deleteContentCategory(ids);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getContentInfo")
    @ResponseBody
    public Result getContentInfo() {

        try {

            List<TbContent> contents = contentService.getContentInfo();

            return Result.result(ConstantEnum.SUCCESS).setData(contents);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("addContent")
    @ResponseBody
    public Result addContent(TbContent tbContent) {

        try {

            contentService.addContent(tbContent);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getContent")
    @ResponseBody
    public Result getContent(Long id) {

        try {

            TbContent tbContent = contentService.getContent(id);

            return Result.result(ConstantEnum.SUCCESS).setData(tbContent);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("updateContent")
    @ResponseBody
    public Result updateContent(TbContent tbContent) {

        try {

            contentService.updateContent(tbContent);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("deleteContent")
    @ResponseBody
    public Result deleteContent(Long[] ids) {

        try {

            contentService.deleteContent(ids);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }
}
